<?php
namespace App\Http\Controllers\Wx;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Rent;
use App\Models\RentLog;
use App\Models\User;
use App\Http\Requests\ApiRequest;

class RentController extends Controller
{
    public function index(ApiRequest $request)
    {
        $user = User::where('small_openid',$request->user->small_openid)->first();
        if(!$user) {
            $rs['status'] = false;
            $rs['msg'] = '无查看权限';
            return response()->json($rs);
        }

        $query = Rent::query(); 

        if($request->keyword) {
            $keyword = trim($request->keyword);
            $query->where('car_num', 'LIKE', '%'.$keyword.'%')
                   ->orWhere('name', 'LIKE', '%'.$keyword.'%')
                   ->orWhere('mobile', 'LIKE', '%'.$keyword.'%');
        }
        
    	   		
    	$list = $query->orderBy('created_at','desc')->paginate(10);
    	$rs['data'] = $list;
    	$rs['status'] = true;
    	return response()->json($rs);

    }

    public function show(ApiRequest $request)
    {
    	$id   = $request->id;
    	$data = Rent::find($id);
    	$rs['data'] = $data;
    	$rs['status'] = true;
    	return response()->json($rs);
    }

    public function log(Request $request)
    {
        $rs['status'] = true;
        $where['rent_id'] = $request->rent_id;
        $list = RentLog::where($where)->get();
        $rs['list'] = $list;
        return response()->json($rs);
    }
}
